package vip.xiaonuo.biz.modular.event.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import vip.xiaonuo.biz.modular.event.entity.BizEvent;
import vip.xiaonuo.biz.modular.event.param.ClientUserEventHandleParam;
import vip.xiaonuo.biz.modular.event.param.EventExcDto;
import vip.xiaonuo.biz.modular.event.param.EventQryDto;
import vip.xiaonuo.biz.modular.event.result.EventPageResult;
import vip.xiaonuo.biz.modular.event.service.BizEventService;
import vip.xiaonuo.common.pojo.CommonResult;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

@Api(tags = "事件控制器")
@ApiSupport(author = "SNOWY_TEAM", order = 1)
@RestController
@Validated
@RequestMapping("/biz/event/**")
public class BizEventController {

    @Resource
    private BizEventService bizEventService;

    @ApiOperationSupport
    @ApiOperation("获取分页信息")
    @PostMapping("/page")
    public CommonResult<Page<EventPageResult>> page(@RequestBody EventQryDto dto) {
        return CommonResult.data(bizEventService.page(dto));
    }

    @ApiOperationSupport
    @ApiOperation("详情")
    @GetMapping("/detail")
    public CommonResult<BizEvent> detail(EventQryDto dto) {
        return CommonResult.data(bizEventService.getById(dto.getEventId()));
    }

    @ApiOperationSupport
    @ApiOperation("处理异常")
    @PostMapping("/excHandle")
    public CommonResult<String> excHandle(@RequestBody ClientUserEventHandleParam param) {
        return bizEventService.handleUserEventForClient(param) ? CommonResult.ok() : CommonResult.error("操作失败");
    }

    @ApiOperationSupport
    @ApiOperation("批量处理异常")
    @PostMapping("/excHandleBatch")
    public CommonResult<String> excHandleBatch(@RequestBody EventExcDto dto) {
        int count = bizEventService.handleExcBatch(dto);
        if (count > 0) {
            return CommonResult.ok();
        } else {
            return CommonResult.error("批量处理异常失败");
        }
    }

    @ApiOperationSupport
    @ApiOperation("盘库报告")
    @GetMapping(value = "/exportInvGoods", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
    public void exportInvGoods(String apiLogId, HttpServletResponse response) {
        bizEventService.exportInvGoods(apiLogId, response);
    }
}
